import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/index",
  },
  {
    path: "/index",
    name: "zhuye",
    redirect: "/home",
    component: () => import("vw/Index/Index"),
    children: [
      // 首页
      {
        path: "/home",
        name: "shouye",
        component: () => import("vw/Index/Home"),
        meta: { keepAlive: true },
      },
      // 分类页
      {
        path: "/classify",
        name: "fenlei",
        component: () => import("vw/Index/Classify"),
      },
      // 菜谱页
      {
        path: "/recipe",
        name: "caipu",
        component: () => import("vw/Index/Recipe"),
      },
      // 购物车页
      {
        path: "/cart",
        name: "gouwuche",
        component: () => import("vw/Index/Cart"),
      },
      // 我的页
      {
        path: "/mine",
        name: "wode",
        component: () => import("vw/Index/Mine"),
        // meta: {
        //   reqireLogin: true,
        // },
      },
    ],
  },
  // 选择地址页
  {
    path: "/address",
    name: "xuanzedizhi",
    component: () => import("vw/Address/Address"),
  },
  // 新增地址页
  {
    path: "/newaddress",
    name: "xinzengdizhi",
    component: () => import("vw/Address/NewAddress"),
  },
  // 消息页
  {
    path: "/news",
    name: "xiaoxi",
    component: () => import("vw/News/News"),
  },
  // 搜索页
  {
    path: "/search",
    name: "sousuo",
    component: () => import("vw/Search/Search"),
  },
  // 详情页
  {
    path: "/detail",
    name: "xiangqing",
    component: () => import("vw/Detail/Detail"),
    meta: { keepAlive: true },
  },
  // 评论页
  {
    path: "/comments",
    name: "pinglun",
    component: () => import("vw/Comments/Comments"),
  },
  // 确认订单页
  {
    path: "/order",
    name: "querendingdan",
    component: () => import("vw/Order/Order"),
  },
  // 客服页
  {
    path: "/waiter",
    name: "kefu",
    component: () => import("vw/Waiter/Waiter"),
  },
  // 优惠券页
  {
    path: "/coupon",
    name: "youhuiquan",
    component: () => import("vw/Coupon/Coupon"),
  },
  //登录页
  {
    path: "/login",
    name: "dengluye",
    component: () => import("vw/Login/Login"),
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  // 路由鉴权
  // 判断去的页面是否需要登录
  if (to.meta.reqireLogin) {
    // 判断现在是否已经登录
    if (localStorage.getItem("token")) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }

  // next()表示放行
  // next(false)表示不放行
  // next("/login")表示重定向
});

export default router;
